package main.java.ensao.cryptography.elgamalmodule;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Random;

public class ElGamal
{
	private BigInteger p, gB, kB, b,Kalpha,C,K,alpha,m;
	public BigInteger getM() {
		return m;
	}


	public void setM(BigInteger m) {
		this.m = m;
	}


	public BigInteger getAlpha() {
		return alpha;
	}


	public void setAlpha(BigInteger alpha) {
		this.alpha = alpha;
	}
	private Random sc;
	
   
  public BigInteger getP() {
		return p;
	}


	public void setP(BigInteger p) {
		this.p = p;
	}


	public BigInteger getgB() {
		return gB;
	}


	public void setgB(BigInteger gB) {
		this.gB = gB;
	}


	public BigInteger getkB() {
		return kB;
	}


	public void setkB(BigInteger kB) {
		this.kB = kB;
	}


	public BigInteger getB() {
		return b;
	}


	public void setB(BigInteger b) {
		this.b = b;
	}


	public BigInteger getKalpha() {
		return Kalpha;
	}


	public void setKalpha(BigInteger kalpha) {
		Kalpha = kalpha;
	}


	public BigInteger getC() {
		return C;
	}


	public void setC(BigInteger c) {
		C = c;
	}


	public BigInteger getK() {
		return K;
	}


	public void setK(BigInteger k) {
		K = k;
	}


	public Random getSc() {
		return sc;
	}


	public void setSc(Random sc) {
		this.sc = sc;
	}


public void calcul_keys(String pp,String gg){
	  // Bob va fournir le g et le p
	 
	  p = new BigInteger(pp);
	 System.out.println("p = " + p);
	 gB = new BigInteger(gg);
	 System.out.println("gB = " + gB);
	 //le programme va g�n�rer une cl� priv�e de Bob
	 b = new BigInteger("12");
	  System.out.println("b = " + b);
	 //le programe va calculer sa cl� publique KB 
	  kB = gB.modPow(b, p);
	  System.out.println("kB = " + kB);
    sc = new SecureRandom();
      
     
	  
   }
  
  public void Generate_Session_Key(String Salpha,String Kb,String pp){
	  
	   alpha = new BigInteger(Salpha);
	  System.out.println("alpha  : "+alpha);
	  //on calcule K
	  K= new BigInteger(Kb).modPow(alpha,new BigInteger(pp));
	  //on entre le message clair
	  System.out.println("K cle  de session est egale � : "+K);
  }
  public void Encryption(String M,String KK,String pp,String gg,String Alpha){
	 
	  //
      // Chiffrement
      //
	  //Alice choisit un alpha
      
    BigInteger m = new BigInteger(M);
      System.out.println("Plaintext = " + m);
     //le programme calcule le chiffr� du message 
      C = m.multiply(new BigInteger(KK)).mod(new BigInteger(pp));
      System.out.println("message chiffr� = " + C);
      // le programme calcule Kaplha afin d'enoyer � Bob (C,Kaplha)
      Kalpha = new BigInteger(gg).modPow(new BigInteger(Alpha),new BigInteger(pp));
     System.out.println("Kalpha = " + Kalpha);
    
    
  }
  
  public  void Decryption(String Cc,String Pp,String KAlpha,String B ){
	  //
      // Dechiffrement
      //
      BigInteger K = new BigInteger(KAlpha).modPow(new BigInteger(B), new BigInteger(Pp));
      BigInteger K_Inverse = K.modInverse(new BigInteger(Pp));
      m = K_Inverse.multiply(new BigInteger(Cc)).mod(new BigInteger(Pp));
      System.out.println("\n\nkB^alpha mod p = " + K);
      System.out.println("inverse de k  = " + K_Inverse);
      System.out.println("message d�chiffr�: " + m);
  }
   
  
}